<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

$return=array();
$tabs=array(
	array('op'=>'list','title'=>'商品列表'),
	array('op'=>'cj','title'=>'ID采集商品'),
	//array('op'=>'urlcj','title'=>'网址采集商品'),
	array('op'=>'listcj','title'=>'批量采集'),
);

if($_RQ['op']=='cj'){
	$set=$this->plugin_setting('basic');	
	if(!$set['secret'] || !$set['client_id']){
		exi('请先设置参数','','/setting/');
	}
	if(!$set['siteid']){
		exi('请先设置参数--推广位ID','','/setting/');
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['sku']){
			exi('商品ID必须填写！','error');
		}
		$post['sku']=explode('|', $post['sku']);
		$post['sku']=ijson_encode($post['sku']);
		$re=$this->model()->getgoodslist($post['sku']);
		if($re){
				if($re['goods_list']){
					foreach($re['goods_list'] as $v){
						$v=(array)$v;
						$this->model()->creategoods($v);
					}
				}else{
					$this->model()->creategoods($re);
				}
				exi('采集成功','');
		}else{
			exi('采集失败','error');
		}
		
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'采集商品',
		'tabs'=>$tabs,
		
		'edit'=>array(
			array('title'=>'[新手必看]、1、先去拼多多开发平台申请https://open.pinduoduo.com/</br>2、申请成功,创建一个应用，等待应用的审核。成功后获得client_id和secret，填到本插件的基本设置</br>3、到https://jinbao.pinduoduo.com/，申请，然后点击《开发者中心》填写，上面得到的client_id，进行绑定！</br>4、绑定成功后--》我要推广--》打开里面的商品，找到浏览器地址：例如：https://jinbao.pinduoduo.com/goods-detail?s=E9H2K3bzkxZPDtb1wvfdFlwlYpy0vg7K_JZ3brKny3，其中：“E9H2K3bzkxZPDtb1wvfdFlwlYpy0vg7K_JZ3brKny3”就是商品skuID，复制商品skuID，粘贴到下面！点击采集！','type'=>'tip'),
			array('name'=>'sku','title'=>'商品ID','type'=>'text','help'=>'商品ID，符合|分割，最多100个'),
					
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='urlcj'){
	$set=$this->plugin_setting('basic');	
	if(!$set['client_id'] || !$set['secret']){
		exi('请先设置参数','','/setting/');
	}
	if($_RQ['postdata']){
		$post=get_postdata();		
		$skuid=$this->model()->hq_taoid($post['sku']);
		if(!$skuid){
			exi('获取商品ID失败，请检查网址是否完整','error');
		}
		//print_r('~~'.$skuid);exit;
		$re=$this->model()->getgoodslist($skuid);
		if($re){
				if($re['goods_list']){
					foreach($re['goods_list'] as $v){
						$v=(array)$v;
						$this->model()->creategoods($v);
					}
				}else{
					$this->model()->creategoods($re);
				}
				exi('采集成功','');
		}else{
			exi('采集失败','error');
		}
		
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'采集商品',
		'tabs'=>$tabs,
		
		'edit'=>array(
			array('title'=>'[新手必看]、1、先去拼多多开发平台申请https://open.pinduoduo.com/</br>2、申请成功,创建一个应用，等待应用的审核。成功后获得client_id和secret，填到本插件的基本设置</br>3、到https://jinbao.pinduoduo.com/，申请，然后点击《开发者中心》填写，上面得到的client_id，进行绑定！</br>4、绑定成功后--》我要推广--》打开里面的商品，找到浏览器地址：例如：https://jinbao.pinduoduo.com/goods-detail?s=E9H2K3bzkxZPDtb1wvfdFlwlYpy0vg7K_JZ3brKny3，其中：“E9H2K3bzkxZPDtb1wvfdFlwlYpy0vg7K_JZ3brKny3”就是商品skuID，复制商品skuID，粘贴到下面！点击采集！','type'=>'tip'),
			array('name'=>'sku','title'=>'商品URL','type'=>'string','help'=>'复制拼多多进宝--我要推广--里面商品的网址'),
					
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='listcj'){
	$set=$this->plugin_setting('basic');	
	if(!$set['client_id'] || !$set['secret']){
		exi('请先设置参数','','/setting/');
	}
	$opt_ids=$this->plugin_setting('opt_ids');
	if(!$opt_ids){
		$opt_ids=$this->model()->getgoods_opts();
		if($opt_ids['goods_opt_list']){
			$this->plugin_setting('opt_ids',$opt_ids['goods_opt_list']);
		}
	}
	$cat_ids=$this->plugin_setting('cat_ids');
	if(!$cat_ids){
		$cat_ids=$this->model()->getgoods_cats();
		if($cat_ids['goods_cats_list']){
			$this->plugin_setting('cat_ids',$cat_ids['goods_cats_list']);
		}
	}
	if($_RQ['postdata']){
		$post=get_postdata();
		if(intval($post['page']) !=$post['page']){
			exi('采集第几页数，必须是整数！','error');
		}
		if($post['type']==0){//常规条件采集
			$re=$this->model()->getgoodslist('',$post);
		}elseif($post['type']==1){//按店铺采集
			$re=$this->model()->getgoodslist_store($post['store_id'],$post);
		}elseif($post['type']==2){//爆款采集
			$re=$this->model()->getgoodslist_baokuan($post);
			$re['goods_list']=$re['list'];
		}elseif($post['type']==3){//主题商品采集
			$re=$this->model()->getgoodslist_huodong($post);
		}
		if($re){
				if(count($re['goods_list'])==0){
					//print_r($re);exit;
					exi('没用采集到任何商品，换个条件试试！','error');
				}
				if($re['goods_list']){
					foreach($re['goods_list'] as $v){
						$v=(array)$v;
						$this->model()->creategoods($v);
					}
				}else{
					$this->model()->creategoods($re);
				}
				exi('采集成功','','list');
		}else{
			exi('采集失败','error');
		}
		
	}
	$htmls=array(
		'type'=>'edit',
		'desc'=>'采集商品',
		'tabs'=>$tabs,
		'edit'=>array(
			array('title'=>'[新手必看]、1、先去拼多多开发平台申请https://open.pinduoduo.com/</br>2、申请成功,创建一个应用，等待应用的审核。成功后获得client_id和secret，填到本插件的基本设置</br>3、到https://jinbao.pinduoduo.com/，申请，然后点击《开发者中心》填写，上面得到的client_id，进行绑定！</br>4、绑定成功后--》我要推广--》打开里面的商品，找到浏览器地址：例如：&goods_id=613214895414，其中：“613214895414”就是商品skuID，复制商品skuID，粘贴到下面！点击采集！','type'=>'tip'),
			array('name'=>'type','title'=>'采集方法','type'=>'radio','options'=>'types'),
			array('name'=>'activity_tags','title'=>'商品活动','type'=>'select','options'=>'activity_tags','show'=>array('type'=>0),'help'=>'商品活动标记ID'),
			array('name'=>'cat_id','title'=>'商品类目','type'=>'select','options'=>'cat_ids','option_value'=>'cat_id','option_title'=>'cat_name','show'=>array('type'=>0),'help'=>'商品类目ID'),
			array('name'=>'is_brand_goods','title'=>'是否为品牌商品','type'=>'switch','show'=>array('type'=>0),'help'=>''),
			array('name'=>'keyword','title'=>'商品关键词','type'=>'string','show'=>array('type'=>0),'help'=>''),
			array('name'=>'merchant_type','title'=>'店铺类型','type'=>'radio','options'=>'merchant_types','show'=>array('type'=>0),'help'=>''),
			array('name'=>'opt_id','title'=>'商品标签类目ID','type'=>'select','options'=>'opt_ids','option_value'=>'opt_id','option_title'=>'opt_name','show'=>array('type'=>0),'help'=>'商品标签类目ID'),
			array('name'=>'range_id','title'=>'区间条件','type'=>'select','options'=>'range_ids','show'=>array('type'=>0),'help'=>''),
			array('title'=>'区间范围','name1'=>'range_from','title1'=>'区间的开始值','unit1'=>'','name2'=>'range_to','title2'=>'区间的结束值','type'=>'string_two','show'=>array('type'=>0),'help'=>''),
			array('name'=>'sort_type','title'=>'排序方式','type'=>'select','options'=>'sort_types','show'=>array('type'=>0),'help'=>''),
			array('name'=>'store_id','title'=>'店铺ID','type'=>'string','show'=>array('type'=>1),'help'=>''),
			array('name'=>'sort_baotype','title'=>'爆款类型','type'=>'radio','options'=>'sort_baotypes','show'=>array('type'=>2),'help'=>''),
			array('name'=>'limit','title'=>'采集数量','type'=>'string','show'=>array('type'=>2),'help'=>''),
			array('name'=>'offset','title'=>'从多少个开始采集','type'=>'string','show'=>array('type'=>2),'help'=>''),
			array('name'=>'theme_id','title'=>'官方主题ID','type'=>'string','show'=>array('type'=>3),'help'=>''),
			array('name'=>'page','title'=>'采集第几页','show'=>array('type'=>array(0,1)),'type'=>'string'),
		)
	);
	$return['merchant_types']=array(0=>'全部',1=>'个人',2=>'企业',3=>'旗舰店',4=>'专卖店',5=>'专营店',6=>'普通店');
	$return['range_ids']=array(
			array('id'=>0,'title'=>'最小成团价'),
			array('id'=>1,'title'=>'券后价'),
			array('id'=>2,'title'=>'佣金比例'),
			array('id'=>3,'title'=>'优惠券价格'),
			array('id'=>4,'title'=>'广告创建时间'),
			array('id'=>5,'title'=>'销量'),
			array('id'=>6,'title'=>'佣金金额'),
			array('id'=>7,'title'=>'店铺描述分'),
			array('id'=>8,'title'=>'店铺物流分'),
			array('id'=>9,'title'=>'店铺服务分'),
			array('id'=>10,'title'=>'店铺描述分击败同行业百分比'),
			array('id'=>11,'title'=>'店铺物流分击败同行业百分比'),
			array('id'=>12,'title'=>'店铺服务分击败同行业百分比 '),
			array('id'=>13,'title'=>'商品分'),
			array('id'=>17,'title'=>'优惠券/最小团购价'),
			array('id'=>18,'title'=>'过去两小时pv'),
			array('id'=>19,'title'=>'过去两小时销量'),
	);
	$return['sort_types']=array(
			array('id'=>0,'title'=>'综合排序'),
			array('id'=>1,'title'=>'按佣金比率升序'),
			array('id'=>2,'title'=>'按佣金比例降序'),
			array('id'=>3,'title'=>'按价格升序'),
			array('id'=>4,'title'=>'按价格降序'),
			array('id'=>5,'title'=>'按销量升序'),
			array('id'=>6,'title'=>'按销量降序'),
			array('id'=>7,'title'=>'优惠券金额排序升序'),
			array('id'=>8,'title'=>'优惠券金额排序降序'),
			array('id'=>9,'title'=>'券后价升序排序'),
			array('id'=>10,'title'=>'券后价降序排序'),
			array('id'=>11,'title'=>'按照加入多多进宝时间升序'),
			array('id'=>12,'title'=>'按照加入多多进宝时间降序 '),
			array('id'=>13,'title'=>'按佣金金额升序排序'),
			array('id'=>14,'title'=>'按佣金金额降序排序'),
			array('id'=>15,'title'=>'店铺描述评分升序'),
			array('id'=>16,'title'=>'店铺描述评分降序'),
			array('id'=>17,'title'=>'店铺物流评分升序'),
			array('id'=>18,'title'=>'店铺物流评分降序'),
			array('id'=>19,'title'=>'店铺服务评分升序'),
			array('id'=>20,'title'=>'店铺服务评分降序'),
			array('id'=>27,'title'=>'描述评分击败同类店铺百分比升序'),
			array('id'=>28,'title'=>'描述评分击败同类店铺百分比降序'),
			array('id'=>29,'title'=>'物流评分击败同类店铺百分比升序'),
			array('id'=>30,'title'=>'物流评分击败同类店铺百分比降序'),
			array('id'=>31,'title'=>'服务评分击败同类店铺百分比升序'),
			array('id'=>32,'title'=>'服务评分击败同类店铺百分比降序'),
	);
	$return['types']=array(0=>'常规条件采集',1=>'按店铺采集',2=>'爆款采集',3=>'主题商品采集');
	$return['sort_baotypes']=array(1=>'实时热销榜',2=>'实时收益榜');
	$return['activity_tags']=array(
			array('id'=>4,'title'=>'限时秒杀'),
			array('id'=>7,'title'=>'百亿补贴'),
			array('id'=>20,'title'=>'行业精选'),
			array('id'=>21,'title'=>'金牌商家'),
			array('id'=>24,'title'=>'品牌高佣'),
			array('id'=>10044,'title'=>'潜力爆品'),	
	);
	$return['cat_ids']=$this->plugin_setting('cat_ids');
	$return['opt_ids']=$this->plugin_setting('opt_ids');
	$return['htmls']=$htmls;
	exi($return);
}
//上架商品
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['pluginname']='jy_pddke';
	//$params['status']='1';
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	if($_RQ['num']){
		$psize=$_RQ['num'];
	}
	$total='';
	$fields=array('id','uniacid','paixu','title','image','price','status','createtime','originalprice');
	$list=pdo_getslice('jy_weishop_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	foreach($list as &$item){
		$urlinfo=pdo_get('jy_pddke_url',array('goodsid'=>$item['id'],'mid'=>1,'uniacid'=>$_SESSION['uniacid']));
		$item=array_merge($urlinfo,$item);
		$item['jyurl']=custom_url('jy_weishop/goods.detail//id='.$item['id'],true);
		$item['rate']=pdo_getcolumn('jy_pddke_goods',array('goodsid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'promotion_rate');
		$item['ratefee']=price($item['price']*$item['rate']/1000);
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('tip'=>'这里的商品会展示到商城，可以到手机端商城查看效果！','data'=>'list','del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			
			array('type'=>'img_string','title'=>'商品名称','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				//array('title'=>'价格：￥','name'=>'price')
				)
			),
			array('name'=>'price','title'=>'价格','type'=>'string'),
			array('name'=>'originalprice','title'=>'原价','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'url','title'=>'默认推广链接','type'=>'ewm'),
			array('name'=>'jyurl','title'=>'本地商品链接','type'=>'ewm'),
			array('name'=>'ratefee','title'=>'佣金','type'=>'string'),
		),
		'action'=>array(	
			array('op'=>'tkl','title'=>'推广链接','type'=>'modal'),
		)),
		'search_one'=>array('name'=>'title','title'=>'商品名称','add'=>false),
	);	
	$return['htmls']=$htmls;
	$return['status']=array('0'=>'下架','1'=>'上架');
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		//获取分类
		$pname=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['pcate']),'title');
		$item['cid']=array('title'=>$pname,'cid'=>$item['ccate']);
	}else{
		$item=array();
	}
	$return['item']=$item;
	$return['item']['param']=json_decode($return['item']['param'],true);
	$return['item']['images']=json_decode($return['item']['images'],true);	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回商品列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'paixu','title'=>'排序','type'=>'string','help'=>'数字越大，排名越靠前'),
					array('name'=>'title','title'=>'商品名称','type'=>'copy'),
					array('name'=>'price','title'=>'商品价格','type'=>'copy'),
					array('name'=>'originalprice','title'=>'原价','type'=>'copy'),
					array('name'=>'cid','title'=>'分类','type'=>'goodgroup','options'=>'cid'),
					array('name'=>'saletype','title'=>'商品属性','type'=>'checkbox','options'=>'saletype'),
					array('name'=>'status','title'=>'上架','type'=>'switch'),
				)
			),
		)
	);
	//获取分类
	$return['pcate']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>0));
	$c=array();
	foreach($return['pcate'] as $v){
		$n=$v['title'];
		$son=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$v['id']));
		if($son){
			foreach($son as $s){
				$d=$s['id'];
				$c[$n][$d]=$s['title'];
			}
		}else{
			$c[$n][0]='';
		}
	}
	$return['cid']=$c;
	$return['saletype']=$this->saletypes;
	$return['status']=$status;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'paixu'=>intval($postdata['paixu']),
		'saletype'=>trim($postdata['saletype']),
		'status'=>intval($postdata['status']),
	);
	//获取分类
	$ccate = intval($postdata['cid']['cid']);
	if($ccate){
		$pcate = pdo_getcolumn('jy_weishop_goods_group',array('id'=>$ccate),'parentid');
		$data['ccate']=$ccate;
		$data['pcate']=$pcate;
	}else{
		$pcate = pdo_getcolumn('jy_weishop_goods_group',array('title'=>$postdata['cid']['title']),'id');
		$data['pcate']=$pcate;
	}
	//如果商品类型为计时/计次商品，默认开启核销
	if($data['type']=='3'){
		$data['isverify']=1;
	}
	$goodsid=intval($postdata['id']);
	if($goodsid){
		pdo_update('jy_weishop_goods',$data,array('id'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
	}else{	
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_weishop_goods',$data);
		$goodsid=pdo_insertid();
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='tkl'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_pddke_url',array('mid'=>1,'goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$item['url']){
		$data=$this->model()->getfeeurl(1,$id,'',true);
		$this->model()->intfeeurl($data);
		$item=pdo_get('jy_pddke_url',array('mid'=>1,'goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$htmls[]=array(
		'type'=>'look',
		'look'=>array(
			array('name'=>'mobile_short_url','title'=>'唤醒拼多多app的推广短链接','type'=>'copy','help'=>''),
			array('name'=>'mobile_url','title'=>'唤醒拼多多app的推广长链接','type'=>'copy','help'=>''),
			//array('name'=>'we_app_icon_url','title'=>'小程序图片','type'=>'image'),
			array('name'=>'we_user_name','title'=>'小程序名称','type'=>'copy','help'=>''),
			array('name'=>'we_app_web_view_short_url','title'=>'唤起微信app推广短链接','type'=>'copy','help'=>''),
			array('name'=>'we_app_web_view_url','title'=>'唤起微信app推广链接','type'=>'copy','help'=>''),
		)
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_weishop_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			$skuid=pdo_getcolumn('jy_pddke_goods',array('goodsid'=>$id,'uniacid'=>$_SESSION['uniacid']),'skuid');
			pdo_delete('jy_pddke_goods',array('skuid'=>$skuid,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}